package cn.net.zhenchan.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;

public class DateUtils {
	static Integer[] weekDays = { 7, 1, 2, 3, 4, 5, 6 };

	private static SimpleDateFormat getDateParser(String pattern)
	{
		return new SimpleDateFormat(pattern);
	}

	public static String returnDateFormat(Date date, String dateFormat)
	{
		SimpleDateFormat df = new SimpleDateFormat(dateFormat);
		return df.format(date);
	}

	public static String longToDate(Long time, String pattern)
	{
		if (time != null)
			return getDateParser(pattern).format(new Date(time));
		else
			return "";
	}

	
	
	public static String longToDate(Long time)
	{
		return getDateParser("yyyy-MM-dd").format(new Date(time));
	}

	public static String longToDate2(Long time)
	{
		return getDateParser("MM-dd HH:mm").format(new Date(time));
	}

	public static String longToDateAll(Long time)
	{
		return getDateParser("yyyy-MM-dd HH:mm:ss").format(new Date(time));
	}

	public static String longToDateAll2(Date date)
	{
		return getDateParser("yyyy-MM-dd HH:mm:ss").format(date);
	}
	
	public static String longToDateAllNew(Long time)
	{
		return getDateParser("yyyyMMddHHmmss").format(new Date(time));
	}

	public static String getCurrentDateString()
	{
		return getDateParser("yyyy-MM-dd HH:mm:ss").format(
				new Date(System.currentTimeMillis()));
	}

	/***
	 * String鍨嬫棩鏈熻浆涓簂ong鍨�
	 * 
	 * @param date
	 *            String鍨嬫棩鏈�
	 * @return long 鏃ユ湡
	 * @throws ParseException
	 */
	public static long dateToLong(String source)
	{
		TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));
		try
		{
			return getDateParser("yyyy/MM/dd").parse(source).getTime();
		}
		catch (ParseException e)
		{
			try
			{
				return getDateParser("yyyy-MM-dd").parse(source).getTime();
			}
			catch (ParseException e1)
			{
				return -1;
			}
		}

	}

	public static long dateAddOneDayAndToLong(String source)
	{
		try
		{
			Date date = getDateParser("yyyy/MM/dd").parse(source);
			Calendar cd = Calendar.getInstance();
			cd.setTime(date);
			cd.add(Calendar.DAY_OF_MONTH, 1);
			return cd.getTime().getTime();
		}
		catch (ParseException e)
		{
			try
			{
				Date date = getDateParser("yyyy-MM-dd").parse(source);
				Calendar cd = Calendar.getInstance();
				cd.setTime(date);
				cd.add(Calendar.DAY_OF_MONTH, 1);
				return cd.getTime().getTime();
			}
			catch (ParseException e1)
			{
				return -1;
			}
		}
	}

	public static long nextDate(String source)
	{
		try
		{
			return getDateParser("yyyy/MM/dd").parse(source).getTime() + 24
					* 60 * 60 * 1000;
		}
		catch (ParseException e)
		{
			try
			{
				return getDateParser("yyyy-MM-dd").parse(source).getTime() + 24
						* 60 * 60 * 1000;
			}
			catch (ParseException e1)
			{
				return -1;
			}
		}

	}

	public static String longToFrontDate(Long time)
	{
		return getDateParser("yyyy-MM-dd").format(
				new Date(time - 24 * 60 * 60 * 1000));
	}

	/***
	 * String鍨嬫棩鏈熻浆涓簂ong鍨�
	 * 
	 * @param date
	 *            String鍨嬫棩鏈�
	 * @return long 鏃ユ湡
	 * @throws ParseException
	 */
	public static long dateAllToLong(String source)
	{
		try
		{
			return getDateParser("yyyy/MM/dd HH:mm:ss").parse(source).getTime();
		}
		catch (ParseException e)
		{
			try
			{
				return getDateParser("yyyy-MM-dd HH:mm:ss").parse(source)
						.getTime();
			}
			catch (ParseException e1)
			{
				return -1;
			}
		}

	}

	/**
	 * 鐢熸垚娴佹按鍙�
	 * 
	 * @return 绫讳技"20090507095515693"鐨勫瓧绗︿覆(15浣�)
	 */
	public static long genSerialNumber()
	{
		return Long
				.valueOf(getDateParser("yyyyMMddHHmmsss").format(new Date()));
	}

	public static long genYMD()
	{
		return Long.valueOf(getDateParser("yyyyMMdd").format(new Date()));
	}

	/**
	 * 鑾峰緱鏃ユ湡
	 */
	public static Date getDate(String birthday)
	{
		return new Date(dateToLong(birthday));
	}

	/**
	 * 鑾峰彇鏃ユ湡瀛楃
	 * 
	 * @param date
	 *            鏃ユ湡
	 * @param format
	 *            鏍煎紡 濡�:yyy-MM-dd
	 * @return
	 */
	public static String getDate(Date date, String format)
	{
		return getDateParser(format).format(date);
	}

	public static Date toDate(String dataString, String format)
			throws ParseException
	{
		return getDateParser(format).parse(dataString);
	}

	/**
	 * 鑾峰緱褰撳墠鏃ユ湡
	 * 
	 * @return
	 */
	public static long getNowDate()
	{
		return DateUtils.dateToLong(getDateParser("yyyy-MM-dd").format(
				new Date()));
	}

	/**
	 * 鑾峰緱褰撳墠鏃ユ湡
	 * 
	 * @return
	 */
	public static long getNowDate(Date curDate)
	{
		return DateUtils
				.dateToLong(getDateParser("yyyy-MM-dd").format(curDate));
	}

	/**
	 * 鑾峰彇褰撳墠鏃堕棿鍔犱笂浠绘剰澶╂暟鍚庣殑鏃ユ湡
	 * 
	 * @param dayNum
	 *            澶╂暟
	 * @return
	 */
	public static String getNewDateByAdd(int dayNum)
	{
		Date date = new Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, dayNum);
		return (new SimpleDateFormat("yyyy-MM-dd")).format(cal.getTime());
	}

	public static String getNewDateByAdd(int dayNum, String format)
	{
		Date date = new Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, dayNum);
		return (new SimpleDateFormat(format)).format(cal.getTime());
	}

	public static Date getDateByAdd(Date date, int num, int timeType)
	{
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(timeType, num);
		return cal.getTime();
	}

	/**
	 * 鑾峰彇鎸囧畾鏃堕棿鍔犱笂浠绘剰灏忔椂鍚庣殑鏃ユ湡
	 * 
	 * @param dayNum
	 *            澶╂暟
	 * @return
	 */
	public static String getNewDateByAddHour(int hour)
	{
		Date date = new Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.HOUR, hour);
		return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(cal
				.getTime());
	}
	
	public static String getNewDateByAddMin(int min){
		Date date = new Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MINUTE, min);
		return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(cal
				.getTime());
	}

	/**
	 * @鑾峰彇褰撳墠鏈堜唤鏈�澶уぉ鏁�
	 * @author lidong
	 * @return 褰撳墠鏈堜唤鏈�澶уぉ鏁�
	 * @2011-12-26
	 */
	public static int lastDayOfMonth()
	{
		Date date = new Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int value = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		return value;
	}

	/**
	 * 寰楀埌鏈夋晥鏈熷ぉ鏁�
	 * 
	 * @param days
	 * @return
	 */
	public static Date addDays(long days)
	{
		Date date = new Date();
		return new Date(date.getTime() + days * 24 * 60 * 60 * 1000);
	}

	public static Date addDays(Date date, long days)
	{
		return new Date(date.getTime() + days * 24 * 60 * 60 * 1000);
	}

	/**
	 * 鑾峰彇鏌愬勾鏌愭湀鐨勬渶鍚庝竴澶�
	 * 
	 * @param year
	 *            骞�
	 * @param month
	 *            鏈�
	 * @return 鏈�鍚庝竴澶�
	 */
	public static int getLastDayOfMonth(int year, int month)
	{
		if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
				|| month == 10 || month == 12)
		{
			return 31;
		}
		if (month == 4 || month == 6 || month == 9 || month == 11)
		{
			return 30;
		}
		if (month == 2)
		{
			if (isLeapYear(year))
			{
				return 29;
			}
			else
			{
				return 28;
			}
		}
		return 0;
	}

	/**
	 * 鏄惁闂板勾
	 * 
	 * @param year
	 *            骞�
	 * @return
	 */
	public static boolean isLeapYear(int year)
	{
		return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
	}

	/**
	 * 鑾峰緱涓嬩釜鏈堟渶鍚庝竴澶╃殑鏃ユ湡
	 * 
	 * @return
	 */
	public String getNextMonthEnd(int da)
	{
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Calendar lastDate = Calendar.getInstance();
		lastDate.add(Calendar.MONTH, da);// 鍔犱竴涓湀
		lastDate.set(Calendar.DATE, 1);// 鎶婃棩鏈熻缃负褰撴湀绗竴澶�
		lastDate.roll(Calendar.DATE, -1);// 鏃ユ湡鍥炴粴涓�澶╋紝涔熷氨鏄湰鏈堟渶鍚庝竴澶�
		str = sdf.format(lastDate.getTime());
		return str;
	}

	/**
	 * 鏈堜唤鐩稿姞
	 * 
	 * @return
	 */
	public String getNextMonth(int da)
	{
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Calendar lastDate = Calendar.getInstance();
		lastDate.add(Calendar.MONTH, da);// 鍔犱竴涓湀
		str = sdf.format(lastDate.getTime());
		return str;
	}

	/**
	 * @鑾峰彇褰撳墠骞翠唤鍙婂線鍓�5骞寸殑骞翠唤闆嗗悎
	 * @author lidong
	 * @return 褰撳墠骞翠唤鍙婂線鍓�5骞寸殑骞翠唤闆嗗悎
	 * @2012-01-11
	 */
	public static List<Object> getYearList()
	{
		List<Object> yearList = new ArrayList<Object>();
		Calendar cal = Calendar.getInstance();
		int currentYear = cal.get(Calendar.YEAR);
		for (int i = 0; i < 5; i++)
		{
			Map<String, Object> yearMap = new HashMap<String, Object>();
			yearMap.put("id", currentYear);
			yearMap.put("text", currentYear + "骞�");
			if (i == 0)
			{
				yearMap.put("selected", true);
			}
			yearList.add(yearMap);
			currentYear = currentYear - 1;
		}
		return yearList;
	}

	/**
	 * 鑾峰彇褰撳墠鏄熸湡
	 * 
	 * @return 鏄熸湡鍑�
	 */
	public static Integer getCurrentWeek()
	{
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
			w = 0;
		return weekDays[w];
	}

	/**
	 * 褰撴湀绗竴澶�
	 * 
	 * @return
	 */
	public static String getFirstDayOfMonth(String formart)
	{
		Calendar cal = Calendar.getInstance();

		SimpleDateFormat datef = new SimpleDateFormat(formart);
		cal.set(GregorianCalendar.DAY_OF_MONTH, 1);
		Date beginTime = cal.getTime();

		return datef.format(beginTime);
	}

	public static long nextDate(String formart, String source)
			throws ParseException
	{
		return getDateParser(formart).parse(source).getTime() + 24 * 60 * 60
				* 1000;
	}

	public static String genHMS()
	{
		return getDateParser("HH:mm:ss").format(new Date());
	}

	public static String longToDate3(long time)
	{
		return getDateParser("MM鏈坉d鏃�").format(new Date(time));
	}

	/**
	 * 鑾峰彇褰撳墠鏄熸湡
	 * 
	 * @return 鍛ㄥ嚑
	 */
	static String[] weekDayStr = { "鍛ㄦ棩", "鍛ㄤ竴", "鍛ㄤ簩", "鍛ㄤ笁", "鍛ㄥ洓", "鍛ㄤ簲", "鍛ㄥ叚" };

	public static String getCurrentWeek(Date date)
	{
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
		{
			w = 0;
		}
		return weekDayStr[w];
	}

	/**
	 * 灏嗙壒瀹氭牸寮忕殑瀛楃涓茶浆鎴恉ate
	 * 
	 * @param date
	 *            20131021 杞垚2013-19-21
	 * @throws ParseException
	 */
	public static String stringToDate(String date) throws ParseException
	{
		if (date.length() == 8)
		{
			date = date.substring(0, 4) + "-" + date.substring(4, 6) + "-"
					+ date.substring(6, 8);
		}
		return date;
	}

	public static void main(String[] args)
	{
		// String str = "";
		// try {
		// str =stringToDate("20130817");
		// System.out.println(str);
		//
		// System.out.println(DateUtils.longToDate(DateUtils.nextDate("yyyy-MM-dd",str)));
		// System.out.println(DateUtils.toDate("2013/10/21 21:00:00", "hh:mm"));
		// } catch (Exception e) {
		// e.printStackTrace();
		// }
		// String str = "20131117";
		// if(str.length()==8){
		// str =
		// str.substring(0,4)+"-"+str.substring(4,6)+"-"+str.substring(6,8);
		// System.out.println("2013/10/21 21:00:00".split(" ")[1]);
		// System.out.println(DateUtils.longToDate(DateUtils.dateToLong("2013/10/21 21:00:00"),"yyyy-MM-dd kk:mm"));
		// }
		//

		System.out.println(new DateUtils().getNextMonth(-1));
		System.out.println(new DateUtils().getNextMonth(0));
		System.out
				.println(new DateUtils().getDate(
						new DateUtils().addDays(new Date(), -1),
						"yyyy-MM-dd HH:mm:ss"));

		// System.out.println(DateUtils.longToDate(new
		// Date().getTime(),"yyyy-MM-dd HH:mm:ss"));

		Date date1 = new Date(System.currentTimeMillis());
		try {
			Thread.sleep(9000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		Date date2 = new Date(System.currentTimeMillis());
		
		System.out.println((int)(date2.getTime() - date1.getTime())/1000);
		
		
	}

	public enum Formats
	{

		ES("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"), NORMAL("yyyy-MM-dd HH:mm:ss"), YEAR_MOUTH_DAY(
				"yyyyMMdd"), SERIALIZE("yyyy_MM_dd_HH_mm_ss");
		private final String STYLE;

		private Formats(String style)
		{
			this.STYLE = style;
		}
	}

	/**
	 * 鑾峰緱鐢ㄤ簬搴忓垪鍖栦紶杈撶殑褰撳墠鏃堕棿
	 * @return
	 */
	public static String formatSerializeDate()
	{
		return formatDate(new Date(), Formats.SERIALIZE);
	}

	public static String formatDate(Object obj)
	{
		return formatDate(obj, Formats.NORMAL);
	}

	public static String formatDate(Object obj, Formats style)
	{
		String dateStr = "";
		if (obj instanceof Date)
		{
			try
			{
				SimpleDateFormat sdf = new SimpleDateFormat(style.STYLE);
				dateStr = sdf.format((Date) obj);
			}
			catch (Exception e)
			{
				dateStr = "";
				e.printStackTrace();
			}
		}
		return dateStr;
	}

	public static String nowTime()
	{
		return nowTime(Formats.ES);
	}

	public static String nowTime(Formats style)
	{
		Date date = new Date();
		String dateStr = null;
		try
		{
			SimpleDateFormat sdf = new SimpleDateFormat(style.STYLE);
			dateStr = sdf.format(date);
		}
		catch (Exception e)
		{
			dateStr = "";
			e.printStackTrace();
		}
		return dateStr;
	}

	public static String nowTimeNormal()
	{
		Date date = new Date();
		String dateStr = null;
		try
		{
			SimpleDateFormat sdf = new SimpleDateFormat(Formats.NORMAL.STYLE);
			dateStr = sdf.format(date);
		}
		catch (Exception e)
		{
			dateStr = "";
			e.printStackTrace();
		}
		return dateStr;
	}

	/**
	 * 鏃ユ湡楠岃瘉
	 * 
	 * @param object
	 * @return
	 */
	public static boolean isDate(Object object)
	{
		return object instanceof Date
				|| Pattern.matches("[1-2][0-9][0-9][0-9]-[0-9][0-9].*",
						object.toString());
	}
}
